$(function() {

	$('.insert_h1').click(function(e) {
		var insert = $('.mod_players_list .insert');
		insert.slideToggle();
	});

	$('.register_h1').click(function(e) {
		var register = $('.mod_players_list .register');
		register.slideToggle();
	});

	$('.season_insert_link').click(function(e) {
		var season_insert = $('.admin_seasons .season_insert');
		season_insert.slideToggle();
	});

	$('.team_insert_link').click(function(e) {
		var team_insert = $('.mod_team_list .team_insert');
		team_insert.slideToggle();
	});

	$('.team_register_link').click(function(e) {
		var team_register = $('.mod_team_list .team_register');
		team_register.slideToggle();
	});
	
	$('#update_modal').on('show.bs.modal', function (event) {
	  	var button = $(event.relatedTarget); 
		var team_id = button.attr('data-team'); 
		var modal = $(this);
		var submit = modal.find('button.submit');

		$.ajax({
			type: "POST",
			url: "ploeg/get_team_by_id",
			data: { id_ploeg: team_id },
			dataType: "json",
			success: function(data) 
			{	
				var arr = $.map(data, function(el) { return el; });
				
				modal.find('form').attr('id', 'update' + team_id );
  				modal.find('input[name=id]').val(arr[0]);
  				modal.find('input[name=naam]').val(arr[1]);
  				modal.find('input[name=straat]').val(arr[2]);
  				modal.find('input[name=nummer]').val(arr[3]);
  				modal.find('input[name=postcode]').val(arr[4]);
  				modal.find('input[name=plaats]').val(arr[5]);
  				modal.find('input[name=thuis]').prop('checked', false);
  				if(arr[8] == 1) {
					modal.find('input[name=thuis]').prop('checked', true);
				}
			}
		});

		submit.click(function(e) {
			modal.find('form').submit();
		});
	})

	$('.submits button.edit').click(function(e) {
		var id_player = $(this).attr("data-idplayer");
		var line = $('#player_'+id_player + ' .update');
		line.slideToggle();

		return false;
	});

	$('.actions button.unregister').click(function(e) {
		var id_subscription = $(this).attr('data-subscription'); 
		var team_id = $(this).attr('data-team'); 

		$.ajax({
			type: "POST",
			url: "ploeg/unregister",
			data: { id: id_subscription },
			success: function() {
				var line = $('#team_'+team_id);
				var select = $('.form_select');
				line.remove();
				select.append('<option value="' + team_id +'">test</option>');
			}
		});
	});

	$('.actions button.delete').click(function(e) {
		var team_id = $(this).attr('data-team'); 

		$.ajax({
			type: "POST",
			url: "ploeg/delete",
			data: { id: team_id },
			success: function() {
				var line = $('#team_'+team_id);
				line.remove();
			}
		});

	});
});
